package com.pb.wkflow.core.listener;

import org.camunda.bpm.application.impl.event.ProcessApplicationEventListenerDelegate;
import org.camunda.bpm.application.impl.event.ProcessApplicationEventParseListener;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.camunda.bpm.engine.impl.pvm.process.ScopeImpl;
import org.camunda.bpm.engine.impl.pvm.process.TransitionImpl;
import org.camunda.bpm.engine.impl.task.TaskDefinition;

/**
 * @author 苗益辉
 * @date 2022/4/26 16:39
 */
public class CamundaGlobalListener extends ProcessApplicationEventParseListener {

    public static final ExecutionListener EXECUTION_LISTENER = new ProcessApplicationEventListenerDelegate();
    public static final TaskListener TASK_LISTENER = new ProcessApplicationEventListenerDelegate();
    private static final ExecutionStartListener EXECUTION_START_LISTENER = new ExecutionStartListener();
    private static final TaskEventListener TASK_EVENT_LISTENER = new TaskEventListener();
    private static final TaskDeleteListener TASK_DELETE_LISTENER = new TaskDeleteListener();
    private static final TaskCompletedListener TASK_COMPLETED_LISTENER = new TaskCompletedListener();
    private static final ExecutionEndListener EXECUTION_END_LISTENER = new ExecutionEndListener();

    /**
     * 事件结束监听
     * @param activity 活动
     */
    @Override
    protected void addEndEventListener(ScopeImpl activity) {
        if ("userTask".equals(activity.getProperty("type"))) {
            activity.addListener(ExecutionListener.EVENTNAME_END, EXECUTION_END_LISTENER);
        } else {
            activity.addListener(ExecutionListener.EVENTNAME_END, EXECUTION_LISTENER);
        }
    }

    /**
     * 事件开始监听
     * @param activity 活动
     */
    @Override
    protected void addStartEventListener(ScopeImpl activity) {
        activity.addListener(ExecutionListener.EVENTNAME_START, EXECUTION_LISTENER);
    }

    /**
     * 领取事件监听
     * @param transition 转变
     */
    @Override
    protected void addTakeEventListener(TransitionImpl transition) {
        transition.addListener(ExecutionListener.EVENTNAME_TAKE, EXECUTION_LISTENER);

    }

    /**
     * 任务分配监听
     * @param taskDefinition 任务定义
     */
    @Override
    protected void addTaskAssignmentListeners(TaskDefinition taskDefinition) {
        taskDefinition.addTaskListener(TaskListener.EVENTNAME_ASSIGNMENT, TASK_LISTENER);
    }

    /**
     * 任务创建监听
     * @param taskDefinition 任务
     */
    @Override
    protected void addTaskCreateListeners(TaskDefinition taskDefinition) {
        taskDefinition.addTaskListener(TaskListener.EVENTNAME_CREATE, TASK_EVENT_LISTENER);
    }

    /**
     * 任务完成监听
     * @param taskDefinition 任务
     */
    @Override
    protected void addTaskCompleteListeners(TaskDefinition taskDefinition) {
        taskDefinition.addTaskListener(TaskListener.EVENTNAME_COMPLETE, TASK_COMPLETED_LISTENER);
    }

    /**
     * 任务更新监听
     * @param taskDefinition 任务
     */
    @Override
    protected void addTaskUpdateListeners(TaskDefinition taskDefinition) {
        taskDefinition.addTaskListener(TaskListener.EVENTNAME_UPDATE, TASK_LISTENER);
    }

    /**
     * 任务删除监听
     * @param taskDefinition 任务
     */
    @Override
    protected void addTaskDeleteListeners(TaskDefinition taskDefinition) {
        taskDefinition.addTaskListener(TaskListener.EVENTNAME_DELETE, TASK_DELETE_LISTENER);
    }
}
